package com.example.config;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author 王鹤蒲
 * @Date 2023 12 21
 */

@Configuration
public class ElasticsearchConfig {

    @Value("${elasticsearch.schema}")
    private String schema;

    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private int port;

    @Value("${elasticsearch.enable}")
    private boolean enable;

    @Value("${elasticsearch.username}")
    private String username;

    @Value("${elasticsearch.password}")
    private String password;

    @Bean
    public ElasticsearchClient elasticsearchClient(){
        ElasticsearchClient client=new ElasticsearchClient(null);
        if(enable){
         final CredentialsProvider credentialsProvider=new BasicCredentialsProvider();
         if(username!=null && !username.isEmpty() && password!=null && !password.isEmpty()){
             credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username,password));
         }
            Header [] defaultHeaders=new Header[]{new BasicHeader("header","value")};
            RestClient restClient=RestClient.builder(new HttpHost(host,port,schema)).setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)).setDefaultHeaders(defaultHeaders).build();
            ElasticsearchTransport transport=new RestClientTransport(restClient,new JacksonJsonpMapper());
            client=new ElasticsearchClient(transport);
        }
     return client;
    }

    @Bean
    public ElasticsearchAsyncClient elasticsearchAsyncClient(){
        ElasticsearchAsyncClient asyncClient=new ElasticsearchAsyncClient(null);
        if(enable){
            final CredentialsProvider credentialsProvider=new BasicCredentialsProvider();
            if(username!=null && !username.isEmpty() && password!=null && !password.isEmpty()){
                credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username,password));
            }
            Header [] defaultHeaders=new Header[]{new BasicHeader("header","value")};
            RestClient restClient=RestClient.builder(new HttpHost(host,port,schema)).setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)).setDefaultHeaders(defaultHeaders).build();
            ElasticsearchTransport transport=new RestClientTransport(restClient,new JacksonJsonpMapper());
            asyncClient=new ElasticsearchAsyncClient(transport);
        }
        return asyncClient;
    }
}
